<script lang="ts" setup>
	import { ref } from "vue";
	import {
		useRouter
	} from 'uni-mini-router';
	import {
		onShow,
		onHide,
		onLoad,
		onReachBottom
	} from '@dcloudio/uni-app'
	import DaDropdown from '@/components/da-dropdown/index.vue'
	import busGoodsCard from "@/components/businessGoodsCard/businessGoodsCard.vue"
	import {
		getCurrentInstance
	} from 'vue'
	import {
		userStore
	} from '@/pinia';
	const userStoreInfo = userStore();
	const {
		proxy,
	} = getCurrentInstance()
	let router = useRouter();
	const routerLink = (e) => {
		//成为会员
		// if (isVip.value) return
		router.push({
			name: e,
			params: {
				obj: JSON.stringify(postObj.value.meiyeSupplier),
			}
		})
	}
	let postObj = ref(null)
	onLoad((options) => {
		console.log(JSON.parse(options.data), "打印options.data");
		postObj.value = JSON.parse(options.data)
		getGoods()
		//获取了商品没商品数据
	})
	onReachBottom(() => {
		pageNum.value++
		getGoods()
	})
	onShow(() => {
		opJuan()
	})
	let busList = ref([])
	let sales = ref()
	let isNew = ref()
	let isPrice = ref()
	const getGoods = () => {
		let data = {
			sales: sales.value,
			isNew: isNew.value,
			isPrice: isPrice.value,
		}
		proxy.$req('GET', proxy.$api.commonGoodsList2, {
			isType: postObj.value.isType,
			storeId: postObj.value.meiyeSupplier.id,
			pageNo: pageNum.value,
			pageSize: 15,
			...data
		}).then((res) => {
			console.log(res, 'busList');
			busList.value = busList.value.concat(res.result.records)
		})
	}
	let isVip = ref(false)
	const opJuan = () => {
		//领卷前判断是否为商家会员
		proxy.$req('GET', proxy.$api.isNowBusVip, {
			storeId: postObj.value.meiyeSupplier.id,
		}).then((res) => {
			console.log(res);
			if (res.result == 1) {
				isVip.value = true
			} else {
				isVip.value = false
			}
		})
	}
	const backRouter = () => {
		router.back()
	}
	let pageNum = ref(1)
	let screenType = ref('')
	const handleConfirm = (v) => {
		// console.log('handleConfirm ==>', v)
		//点击筛选
		console.log(v);
		sales.value = ''
		isNew.value = ''
		isPrice.value = ''
		pageNum.value = 1
		busList.value = []
		let key = Object.keys(v)[0]
		if (key == 'isPrice' && v[key] == 'asc') {
			isPrice.value = 0
		} else if (key == 'isPrice' && v[key] == 'desc') {
			isPrice.value = 1
		} else if (key == 'sales') {
			sales.value = 1
		} else if (key == 'isNew') {
			isNew.value = 1
		}
		console.log(screenType.value);
		// if (!v[key]) {
		// 	orderByType.value = ''
		// 	sortType.value = ''
		// } else {
		// 	orderByType.value = v[key].toUpperCase()
		// }
		dropdownMenuList.value.forEach((item, index, arr) => {
			// console.log(item);
			if (key !== item.prop) {
				//value是控制箭头状态
				//isActived是控制文字状态
				delete dropdownMenuList.value[index].value
				delete dropdownMenuList.value[index].isActived
			}
		})
		getGoods()
	}
	const handleClose = (v) => {
		// console.log('handleClose ==>', v)
	}
	const handleOpen = (v) => {
		console.log('handleOpen ==>', v)
	}
	const DaDropdownRef = ref()
	const dropdownMenuList = ref([
		{
			title: '综合',
			type: 'click',
			prop: 'all',
			value: true
		},
		{
			title: '销量',
			type: 'click',
			prop: 'sales',
		},
		{
			title: '最新',
			type: 'click',
			prop: 'isNew',
		},
		{
			title: '价格',
			type: 'sort',
			prop: 'isPrice',
		},
	])
	const collectBus = () => {
		if (!postObj.value.meiyeSupplier.isColl) {
			//收藏
			proxy.$req('POST', proxy.$api.meiyeCollection2Add, {
				colType: 2,
				colProductId: postObj.value.meiyeSupplier.id,
			}).then((res) => {
				if (res.success) {
					//收藏成功---缺少收藏字段
					postObj.value.meiyeSupplier.isColl = res.result.colId
				}
			})
		} else {
			//取消收藏
			proxy.$req('DELETE', proxy.$api.meiyeCollection2Delete, {
				id: postObj.value.meiyeSupplier.isColl,
			}).then((res) => {
				if (res.success) {
					postObj.value.meiyeSupplier.isColl = 0
				}
			})
		}
	}
</script>
<template>
	<view class="navbar">
		<u-navbar :placeholder="true" leftText="" title="" auto-back height="110rpx">
			<template #left>
				<view class="r-flex-center-start" @click="backRouter">
					<u-icon name="arrow-left" size="20" style="margin-right: 20rpx;"></u-icon>
					<view class="c-flex-start-start">
						<text style="ont-size: 36rpx;font-weight: 500;">{{ postObj.meiyeSupplier.storeName }}</text>
						<text class="u-line-1"
							style="font-size: 24rpx;font-weight: 400;color: rgba(0,0,0,0.65);">{{ postObj.meiyeSupplier.storeDescription }}</text>
					</view>
				</view>
			</template>
			<template #right>
				<view class="r-flex-center-center">
					<text
						style="border-radius: 30rpx;font-size: 20rpx;background: linear-gradient(137deg, #693326 0%, #CC6F88 100%);color: #fff;width: 128rpx;height: 40rpx;line-height: 40rpx;text-align: center;margin-right: 10rpx;"
						@click="routerLink('goodsBusinessVip')">{{ isVip?'店铺会员':'成为会员' }} </text>
					<view class="" @click="collectBus" v-if="postObj!=null">
						<u-icon :name="!postObj.meiyeSupplier.isColl?'star':'star-fill'" size="40rpx"
							:color="!postObj.meiyeSupplier.isColl?'':'#F4CF5A'"></u-icon>
					</view>
				</view>
			</template>
		</u-navbar>
	</view>
	<view class="screenContainer">
		<DaDropdown ref="DaDropdownRef" v-model:dropdownMenu="dropdownMenuList" themeColor="#CB645E" textColor="#BFBFBF"
			:duration="300" :fixedTop="false" :menuActiveText="false" @confirm="handleConfirm" @close="handleClose"
			@open="handleOpen">
		</DaDropdown>
	</view>
	<view class="aaa" v-if="busList.length>0">
		<bus-goods-card :listt="busList"></bus-goods-card>
	</view>
	<view class="" style="margin-top: 150rpx;" v-else>
		<u-empty mode="list">
		</u-empty>
	</view>
</template>
<style lang="scss">
	page {
		background-color: #F6F6F6;
	}

	.aaa {
		width: 686rpx;
		margin: 0 auto;
	}

	.screenContainer {
		width: 100%;
		height: 100rpx;
		// background-color: #000;
		// border: 1rpx solid #000;
	}

	.navbar {
		width: 100%;
		height: 110rpx;
	}
</style>